<template>
  <dt-modal-edit ref='edit' :title='title' :loading="loading" :label-width='130' :width='500' @ok='onOk'>
  </dt-modal-edit>
</template>

<script>
  import initiateOpt from '../opt/initiate.js'
  import initiateMgr from '@/biz/commission/initiate.js'
  import moment from 'moment'
  
  export default {
    data() {
      return {
        info: {
          commissionName: null,
          commissionMonth: moment().format('YYYY-MM'),
          settlementType: null
        },
        title: '请佣发起',
        loading: false,
        isUpdate: false
      }
    },
    mounted() {
    },
    methods: {
      create() {
        this.isUpdate = false
        this.title = '请佣发起'
        this.$refs.edit.show(initiateOpt.options(this), this.info)
      },
      update(info) {
        this.isUpdate = true
        this.title = '请佣修改'
        this.info = info
        this.$refs.edit.show(initiateOpt.options(this, true), this.info)
      },
      hide() {
        this.$refs.edit.hide()
      },
      onOk() {
        this.loading = true
        if (this.isUpdate) {
          this.onUpdate()
        } else {
          this.onCreate()
        }
      },
      onCreate() {
        initiateMgr.create(this.info).then(res => {
          this.loading = false
          this.$emit('success', res)
          this.hide()
        }).catch(() => {
          this.loading = false
        })
      },
      onUpdate() {
        initiateMgr.update({
          commissionId: this.info.commissionId,
          commissionName: this.info.commissionName,
          commissionMonth: this.info.commissionMonth
        }).then(res => {
          this.loading = false
          this.$emit('success', res)
          this.hide()
        }).catch(() => {
          this.loading = false
        })
      },
    }
  }
</script>

<style>
</style>
